<!-- 登录页 -->
<template>
  <div class="container">
    <div class="form">
      <!-- 标题部分 -->
      <div class="title">
        <span>千锋后台管理</span>
      </div>
      <!-- 表单部分 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="ruleForm.pwd"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 引入http 请求
import { loginApi } from "@/http/http";
// 引入cookie
import { setCookie } from "@/utils/cookie.js";
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        pwd: "",
      },
      rules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      // 登录事件
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //验证通过 调用登录接口
          loginApi(this.ruleForm).then((res) => {
            console.log(22, res);
            if (res.code == 200) {
              // 登录成功之后
              // 将后台生成的token存到本地cookie 中
              setCookie("token", res.data.token);
              // 将角色role 存到本地，以方便其他页面中可以拿到的role
              localStorage.setItem("role", res.data.role);
              this.$message({
                message: "登录成功",
                type: "success",
              });
              setTimeout(() => {
                this.$router.push("/");
              }, 1000);
            } else {
              this.$message("账户不存在！");
            }
          });
          // console.log(this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      // 重置事件
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped lang='less'>
/* @import url(); 引入css类 */
.container {
  width: 100%;
  height: 100%;
  background: url("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp.nanrenwo.net%2Fuploads%2Fallimg%2F190815%2F8525-1ZQ5160136-50.jpg&refer=http%3A%2F%2Fp.nanrenwo.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639818901&t=1a531f13c8c7535b6c46e233438b2967")
    no-repeat center;
  background-size: cover;
  .form {
    width: 370px;
    height: 298px;
    padding: 5px 10px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .title {
      width: 100%;
      line-height: 50px;
      text-align: center;
      font-size: 18px;
      font-weight: bold;
    }
    /deep/.ivu-form {
      width: 300px;
    }
    /deep/.ivu-btn {
      width: 300px;
      height: 30px;
    }
  }
}
</style>